﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PokeTD
{
    public struct Particle
    {
        public Vector2 Position;
        public Vector2 Velocity;
        public Color Color;
        public float Lifetime;
        public float Delay;
    }

    public class ParticleSystem
    {
        List<Particle> Particles;
        Texture2D PixelTexture;
        Random Random = new Random();

        public ParticleSystem()
        {
            Particles = new List<Particle>();
        }

        public void AddParticle(Particle p)
        {
            Particles.Add(p);
        }

        public void AddTexture(Texture2D t, Vector2 pos, float lifetime, float delay, int speed, Rectangle rect)
        {
            Color[] Data = new Color[t.Width * t.Height];
            t.GetData<Color>(Data);
            int w = rect.Width;
            int h = rect.Height;
            for (int y = rect.Y; y < rect.Y + rect.Height; y++)
            {
                for (int x = rect.X; x < rect.X + rect.Width; x++)
                {
                    if (rect.Contains(x, y))
                        AddParticle(
                            pos,// + new Vector2(x - w / 2, y - h / 2),
                            new Vector2(
                                Random.Next((x < rect.X + w / 2) ? -speed : 0, (x < rect.X + w / 2) ? 0 : speed),
                                Random.Next((y < rect.Y + h / 2) ? -speed : 0, (y < rect.Y + h / 2) ? 0 : speed)),
                            Data[y * t.Width + x],
                            (float)(lifetime * (Random.NextDouble() * 2)),
                            delay
                            );
                }
            }
        }
        public void AddTexture(Texture2D t, Vector2 pos, float lifetime, float delay, int speed=25)
        {
            AddTexture(t, pos, lifetime, delay, speed, new Rectangle(0, 0, t.Width, t.Height));
        }

        public void AddParticle(Vector2 pos, Vector2 vel, Color c, float time, float delay = 0.0f)
        {
            Particle p = new Particle();
            p.Position = pos;
            p.Velocity = vel;
            p.Color = c;
            p.Lifetime = time;
            p.Delay = delay;
            AddParticle(p);
        }

        public void Update(GameTime gameTime)
        {
            for (int i = 0; i < Particles.Count; i++)
            {
                Particle p = Particles[i];
                if (p.Delay > 0)
                {
                    p.Delay -= (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                    Particles[i] = p;
                }
                else
                {
                    p.Lifetime -= (float)gameTime.ElapsedGameTime.TotalSeconds;
                    p.Position += p.Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    if (p.Lifetime <= 0)
                    {
                        Particles.RemoveAt(i);
                        i--;
                    }
                    else
                        Particles[i] = p;
                }
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (PixelTexture == null)
            {
                PixelTexture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1);
                PixelTexture.SetData<Color>(new Color[] { Color.White });
            }
            foreach (Particle p in Particles)
            {
                spriteBatch.Draw(PixelTexture, new Rectangle((int)p.Position.X, (int)p.Position.Y, 1, 1), p.Color);
            }
        }
    }
}
